package com.property.demo.repository;

import com.property.demo.model.RolePermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface RolePermissionRepository extends JpaRepository<RolePermission, Long> {
    
    List<RolePermission> findByRoleId(Long roleId);
    
    List<RolePermission> findByPermissionId(Long permissionId);
    
    List<RolePermission> findByRoleIdAndPermissionId(Long roleId, Long permissionId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM RolePermission rp WHERE rp.roleId = ?1")
    void deleteByRoleId(Long roleId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM RolePermission rp WHERE rp.permissionId = ?1")
    void deleteByPermissionId(Long permissionId);
    
    @Transactional
    @Modifying
    @Query("DELETE FROM RolePermission rp WHERE rp.roleId = ?1 AND rp.permissionId = ?2")
    void deleteByRoleIdAndPermissionId(Long roleId, Long permissionId);
} 